<?php namespace Common\Model; 
use Hdphp\Model\Model;
use Common\Model\GoodsDetail;
use Common\Model\GoodsAttr;
use Common\Model\GoodsList;
//后台Goods模型
class Goods extends Model{
	//指定cate表名
	protected $table = 'goods';
	//模型验证
	protected $validate = array(
		//array(字段名,验证方法,错误信息,验证条件,验证时间)
		array('gname','required','名称不能为空',3,3)
	); 
	
	//自动完成
	protected $auto = array(
		//array(表单字段名,处理方法,方法类型,验证条件,处理时间) 
		array('putTime','time','function',3,1),  	//上传时间
		array('admin_id','getAid','method',3,1)    //当前管理员
	);
	
	//设置当前管理员id
	public function getAid(){
		return $_SESSION['info']['aid'];
	}
	
	//添加
	public function store(){
		/*一：验证*/
		//调用check公共方法
		if(!$this->check()) return false;
		/*二：添加*/	
		//1.获取商品	goods表gid，同时goods表的添加		目的：用于商品表与商品详情表关联
		$gid = $this->add();
		//2.添加商品属性	goods_attr表
		$goodsAttrModel = new GoodsAttr;	
		//按属性添加
		foreach(q('post.attr') as $trid=>$v){
			if($v){
				$data = [
					'gvalue'=>$v,
					'type_attr_trid'=>$trid,
					'goods_gid'=>$gid
				];
				$goodsAttrModel->add($data);
			}
		}
		//规格添加
		$spec = q('post.spec');
		if($spec){		
			$value = $spec['value'];
			foreach($value as $trid=>$v){
				foreach($v as $kk=>$vv){
					$data = [
						'gvalue'=>$vv,
						'type_attr_trid'=>$trid,
						'goods_gid'=>$gid,
						'add_price'=>$spec['add_price'][$trid][$kk]						
					];
					$goodsAttrModel->add($data);
				}
			}
		}					
		//3.商品详情	goodsdetail表
		$detailData= new GoodsDetail;
		$detailData->create();
		//商品	 goods表与商品详情	 goods_detail表关联
		$detailData->data['goods_gid'] = $gid;
		//若多张图片上传，则以'|'分开
		$detailData->data['picture'] = implode('|', $detailData->data['picture']);		
		$detailData->add();
		
		return true;
	}	
	
	//执行编辑
	public function edit(){
//		p(q('post.'));
		/*一：验证*/
		//调用check公共方法
		if(!$this->create()) return false;

		/*二：添加*/	
		//1.修改商品列表图片	若修改时图片被删除，则相应的字段赋给空		
		if(!q('post.pic')){
			$this->data['pic'] ='';
		}	
		$this->save($this->data);
		
		//获取当前gid
		$gid = q('post.gid');		
		//2.添加商品属性	goods_attr表
		$goodsAttrModel = new GoodsAttr;	
		//先执行删除
		$goodsAttrModel->where("goods_gid",$gid)->delete();
		//按属性添加
		foreach(q('post.attr') as $trid=>$v){
			if($v){
				$data = [
					'gvalue'=>$v,
					'type_attr_trid'=>$trid,
					'goods_gid'=>$gid
				];
				$goodsAttrModel->add($data);
			}
		}
		//规格添加
		$spec = q('post.spec');
		if($spec){		
			$value = $spec['value'];
			foreach($value as $trid=>$v){
				foreach($v as $kk=>$vv){
					$data = [
						'gvalue'=>$vv,
						'type_attr_trid'=>$trid,
						'goods_gid'=>$gid,
						'add_price'=>$spec['add_price'][$trid][$kk]						
					];
					$goodsAttrModel->add($data);
				}
			}
		}	
							
		//3.商品详情	goodsdetail表
		$detailData= new GoodsDetail;
		$detailData->create();
		//商品详细图片	同上
		if(!q('post.picture')){
			$detailData->data['picture'] ='';
		}	
		//商品	 goods表与商品详情	 goods_detail表关联
		$detailData->data['goods_gid'] = $gid;
		//若没有图片上传，则不执行拆分数组操作
		if(q('post.picture')){
			//若多张图片上传，则以'|'分开
			$detailData->data['picture'] = implode('|', $detailData->data['picture']);
		}
		$detailData->save($detailData->data);
		$detailData->save();
		
		//4.删除货品清单
		$detailData= new GoodsList;
		$detailData->where('goods_gid',$gid)->delete();
		return true;
	}
	
	//验证表
	public function check(){
		//1.验证商品	goods表(本表)
		if(!$this->create()) return false;		
		//2.验证商品详情	goodsdetail表
		$detailData= new GoodsDetail;
		if(!$detailData->create()){
			//把$detailData的错误压入到当前模型中，这样外面的控制器才能获取到数据
			$this->error = $detailData->getError();
			return false;
		}
		return true;
	}

}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	